const mysql = require("mysql");

const createMysql = (config) => async (ctx,next) => {
    const connect =  mysql.createConnection({
        port:"3306",
        host:"localhost",
        user:"admin",
        ...config
    });

    ctx.searchQuery = (query) => {
        connect.connect();
        return new Promise((resolve,reject) => {
            connect.query(query, function (error, results, fields) {
                if (error){
                    reject(error);
                    return;
                };
                resolve(results);
                connect.end();
            });
        })
    }

    await next();
}

module.exports = createMysql;